<?php
class RCMSRequest {
	protected $baseUrl = null;
	protected $pathInfo = null;

	public function isSsl() {
		if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
			return true;
		}
		return false;
	}
	public function isPost(){
		return ($_SERVER['REQUEST_METHOD'] === 'POST');
	}
	public function getGet($name, $default = null) {
		if (isset($_GET[$name])) {
			return $_GET[$name];
		}
		return $default;
	}
	public function getPost($name, $default = null) {
		if (isset($_POST[$name])) {
			return $_POST[$name];
		}
		return $default;
	}
	public function getHost(){
		if (!empty($_SERVER['HTTP_HOST'])) {
			return $_SERVER['HTTP_HOST'];
		}
		return $_SERVER['SERVER_NAME'];
	}

	public function getRequestUri(){
		/*
		 if ($this->requestUri === null) {
			$this->requestUri = urldecode($_SERVER['REQUEST_URI']);
			}
			return $this->requestUri;
			*/
		return $_SERVER['REQUEST_URI'];
	}

	public function getBaseUrl(){
		// can not end with / and not including script filename
		if ($this->baseUrl === null) {
			$pos = strrpos($_SERVER['SCRIPT_NAME'], '/');
			$this->baseUrl = ($pos === false) ? $_SERVER['SCRIPT_NAME'] : substr($_SERVER['SCRIPT_NAME'], 0, $pos);
		}
		return $this->baseUrl;
	}

	public function getPathInfo(){
		if ($this->pathInfo === null) {
			$requestUri = $this->getRequestUri();
			if (false !== ($pos = strpos($requestUri, '?'))) {
				$requestUri = substr($requestUri, 0, $pos);
			}
			$len = strlen($this->getBaseUrl());
			if ($len > 0) {
				$requestUri = substr($requestUri, $len);
			}
			if (false !== ($pos = strrpos($_SERVER['SCRIPT_NAME'], '/'))) {
				$scriptFile = substr($_SERVER['SCRIPT_NAME'], $pos + 1);
				if (1 === (strpos($requestUri, $scriptFile))) {
					$requestUri = substr($requestUri, strlen($scriptFile) + 1);
				}
			}
			$this->pathInfo = urldecode($requestUri);
		}
		return $this->pathInfo;
	}

	public function requestParameters(){
		return array(
    		"ssl"=>$this->isSsl(),
    		"host"=>$this->getHost(),
    		"requestUri"=>$this->getRequestUri(),
    		"baseUrl"=>$this->getBaseUrl(),
    		"pathInfo"=>$this->getPathInfo(),
    		"scriptName"=>$_SERVER['SCRIPT_NAME'],
    		"fileName"=>$_SERVER['SCRIPT_FILENAME'],
		);
	}
}